package com.nageoffer.shortlink.admin.config;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: RBloomFilterConfiguration
 * Package: com.nageoffer.shortlink.admin.config
 * Description:
 *
 * @Author Jetson
 * @Create 2024/10/12 18:05
 * @Version 1.0
 */
@Configuration
public class RBloomFilterConfiguration {

    /**
     * 防止用户注册查询数据库的布隆过滤器
     */
    @Bean
    public RBloomFilter<String> userRegisterCachePenetrationBloomFilter(RedissonClient redissonClient) {
        RBloomFilter<String> cachePenetrationBloomFilter = redissonClient.getBloomFilter("userRegisterCachePenetrationBloomFilter");
        /**
         * 预估布隆过滤器存储元素的大小
         * 运行的误判率
         * 误判率越低，位数组越长，布隆过滤器的内存占用越大
         * 错误率越低，散列Hash函数越多，计算时间较长
         */
        cachePenetrationBloomFilter.tryInit(100000000L, 0.001);
        return cachePenetrationBloomFilter;
    }
}
